package com.freedy.concurrent.Monitors.biaslock;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

import java.io.IOException;

/**
 * 测试偏向锁
 * 添加虚拟机参数 -XX:BiasedLockingStartupDelay=0
 * @author Freedy
 * @date 2021/4/21 20:16
 */
@Slf4j
public class Test1 {
    public static void main(String[] args) throws Exception {
        Thread.sleep(5000);
        Dog d = new Dog();
        ClassLayout classLayout = ClassLayout.parseInstance(d);
        new Thread(() -> {
            log.debug("synchronized 前");
            System.out.println(classLayout.toPrintable());
            synchronized (d) {
                log.debug("synchronized 中");
                System.out.println(classLayout.toPrintable());
            }
            log.debug("synchronized 后");
            System.out.println(classLayout.toPrintable());
        }, "t1").start();
    }
}
class Dog {}
